<?php

/**
 * @Author:      lyt
 * @DateTime:    2016-02-29 16:07:17
 * @Description: 用于出库的各种操作
 */
class ScsDatabaseOutController extends BaseController
{
    /**
     * @Author:      lyt
     * @DateTime:    2016-02-29 16:07:42
     * @Description: 用于创建 出库退货或者 领用出库的 接口
     */
    public function createOut(){

        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
//                'date'=>"required|date",
                'type'=>"required|in:1,2",
                'consuming_id'=>"required",
                'consuming_type'=>"required|in:1,2",
                'cust_id'=>"required",
                'out_type'=>"required|in:1,2",
//                'shop_type'=>"required"
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            if(MaintenanceInventoryList::isInventory($sc_user->shop_id)){
                $response = Lang::get("scs_error.data_inventory_isrun_61000");
                break;
            }

            // //临时兼容前端中文日期传参
            // preg_match_all('/\d/',Input::get("date"),$arr);
            // $date=implode('',$arr[0]);

            $msl=new MaintenanceOutList;
            $id=Common::createId();
            $msl->id=$id;
            $msl->shop_id=$sc_user->shop_id;
            $msl->date=Input::get("date");
            $msl->type=Input::get("type"); //这里包含 两个类型 领用 和 退库
            $msl->consuming_id=Input::get("consuming_id");
            $msl->consuming_type=Input::get("consuming_type");
            if(Input::get("consuming_type")==1){
                if(Input::get('shop_type')==1){//1本店2店外
                    $msl->user_type=Input::get("user_type");
                    if(Input::get('user_type')==1){
                        $sc=SCUser::find(Input::get('consuming_id'));
                        $msl->team=$sc->team;
                    }else{
                        $me=MaintenanceEngineers::find(Input::get('consuming_id'));
                        $msl->team=$me->team;
                    }
                }
            }
            $msl->shop_type=Input::get("shop_type");
            $msl->out_type=Input::get("out_type");
            if($msl->type==2){//如果是 退货出库 需要 退库的相关属性
                $msl->pay_type=Input::get("pay_type");
                $msl->return_type=Input::get("return_type");
            }else{//如果是 领用
                $msl->methed=Input::get("methed");
            }
            $msl->status=0;
            $msl->cust_id=Input::get("cust_id");
            $msl->desc=Input::get("desc");
            $msl->create_by=$sc_user->id;
            $msl->save();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("id"=>$id));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "createOut", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-03-02 09:45:02
     * @Description: 用于删除 出库单的接口
     *  新创建的出库单 没有执行的 没有取消 删除的 可以删除 同时 更新 该出库单下所有的 出库耗材批次记录为 删除
     */
    public function  deleteOut(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id'=>"required",
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $mol=MaintenanceOutList::find(Input::get("id"));
            if(is_null($mol)||$mol->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.maintenance_out_notexit_60002");
                break;
            }
            if($mol->status!=0){
                $response = Lang::get("scs_error.maintenance_out_statuserror_60003");
                break;
            }
            if(MaintenanceInventoryList::isInventory($sc_user->shop_id)){
                $response = Lang::get("scs_error.data_inventory_isrun_61000");
                break;
            }
            $mol->status=3;
            $mol->save();
            MaintenanceOutItem::where("list_id",$mol->id)->where("status",0)->update(array("status"=>2));
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("id"=>$mol->id));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "deleteStorage", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-03-02 10:45:16
     * @Description: 用于执行 出库单的接口
     */
    public function  implementOut(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id'=>"required",
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $mol=MaintenanceOutList::find(Input::get("id"));
            if(is_null($mol)||$mol->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.maintenance_out_notexit_60002");
                break;
            }
            if($mol->status!=0){
                $response = Lang::get("scs_error.maintenance_out_statuserror_60003");
                break;
            }
            if(MaintenanceInventoryList::isInventory($sc_user->shop_id)){
                $response = Lang::get("scs_error.data_inventory_isrun_61000");
                break;
            }
            $response=MaintenanceOutList::implement($mol,$sc_user->id);
            // $response = array('code' => 0, 'msg' => 'ok', 'result' => array("id"=>$msl->id));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "deleteStorage", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-03-01 15:15:16
     * @Description: 用于出库单 的耗材批次
     */
    public function addOutItem(){

        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'list_id'=>"required",
                'item_id'=>"required",
                "number"=>"required|numeric|min:0",
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $mol=MaintenanceOutList::find(Input::get("list_id"));
            if(is_null($mol)||$mol->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.maintenance_out_notexit_60002");
                break;
            }
            if($mol->status!=0){
                $response = Lang::get("scs_error.maintenance_out_statuserror_60003");
                break;
            }
            $msi=MaintenanceStorageItem::find(Input::get("item_id"));
            if(is_null($msi)||$msi->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.maintenance_sitem_notexit_60004");
                break;
            }
            if($msi->status!=1||$msi->type!=1){//如果要出库的 入库批次 状态异常
                $response = Lang::get("scs_error.maintenance_sitem_statuserror_60005");
                break;
            }
            $tem_number=MaintenanceOutItem::where("list_id",Input::get("list_id"))->where("storage_id",$msi->id)->where("status",0)->sum("number");
            if(($msi->current_number-$tem_number)<Input::get("number")){
                $response = Lang::get("scs_error.maintenance_out_numbererror_60006");
                break;
            }
            if(MaintenanceInventoryList::isInventory($sc_user->shop_id)){
                $response = Lang::get("scs_error.data_inventory_isrun_61000");
                break;
            }
            $moi=new MaintenanceOutItem;
            $moi->list_id=Input::get("list_id");
            $moi->storage_list_id=$msi->list_id;
            $moi->storage_id=$msi->id;
            $moi->code=$msi->code;
            $moi->name=$msi->name;
            $moi->model=$msi->model;
            $moi->tax_purchase_price=$msi->tax_purchase_price;
            $moi->no_tax_purchase_price=$msi->no_tax_purchase_price;
            $moi->tax_purchase_subtotal=$msi->tax_purchase_price*Input::get("number");
            $moi->number=Input::get("number");
            $moi->subject=$msi->subject;
            $moi->location=$msi->location;
            $moi->production_date=$msi->production_date;
            $moi->own_id=$msi->own_id;
            $moi->created_by=$sc_user->id;
            $moi->data_id=$msi->data_id;
            $moi->shop_id=$sc_user->shop_id;
            $moi->item_type=$msi->item_type;
            $moi->item_id=$msi->item_id;
            $moi->pack_unit=$msi->pack_unit;
            $moi->sale_amount=Input::get("sale_amount")*100;
            $moi->status=0;
            $moi->save();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("id"=>$moi->id));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "addOutItem", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

   /**
    * @Author:      lyt
    * @DateTime:    2016-03-02 09:55:18
    * @Description: 用于删除 出库批次的接口
    */
    public function deleteOutItem(){

        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                "id"=>'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $moi=MaintenanceOutItem::find(Input::get("id"));
            if(is_null($moi)||$moi->shop_id!=$sc_user->shop_id){
                $response = Lang::get("scs_error.maintenance_outitem_notexit_60008");
                break;
            }
            if($moi->status!=0){
                $response = Lang::get("scs_error.maintenance_outitem_statuserror_60007");
                break;
            }
            if(MaintenanceInventoryList::isInventory($sc_user->shop_id)){
                $response = Lang::get("scs_error.data_inventory_isrun_61000");
                break;
            }
            $moi->status=2;
            $moi->save();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("id"=>$moi->id));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "deleteOutItem", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-03-03 13:26:48
     * @Description: 用于执行 订单出库请求的 接口
     */
    public function  implementOutOrder(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'ids'=>"required",
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $ids = json_decode(Input::get('ids'),1);
            if (count($ids) != count(array_unique($ids))) {
                $response = Lang::get("scs_error.order_out_error_40070");
                break;
            }
            $out_order_arr=array();
            foreach ($ids as $_id) {
                $date_id=explode(":",$_id);
                $moo=MaintenanceOutOrder::find($date_id[0]);
                if(is_null($moo)||$moo->shop_id!=$sc_user->shop_id){
                    $response = Lang::get("scs_error.maintenance_out_order_notexit_60009");
                    break 2;
                }
                if($moo->status!=0){
                    $response = Lang::get("scs_error.maintenance_out_order_statuserror_60010");
                    break 2;
                }
                $moo->temp_storage_id=$date_id[1];
                $out_order_arr[]=$moo;
            }
            if(sizeof($out_order_arr)<=0){
                $response = Lang::get("scs_error.maintenance_out_order_empty_60015");
                break;
            }
            if(MaintenanceInventoryList::isInventory($sc_user->shop_id)){
                $response = Lang::get("scs_error.data_inventory_isrun_61000");
                break;
            }
            $response=MaintenanceOutList::implementOrder($out_order_arr,$sc_user);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "implementOutOrder", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Author:      lyt
     * @DateTime:    2016-03-07 15:30:07
     * @Description:
     */
    public function  refuseOutOrder(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'ids'=>"required",
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.station_args_error_31001");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
//             if(MaintenanceInventoryList::isInventory($sc_user->shop_id)){
//                 $response = Lang::get("scs_error.data_inventory_isrun_61000");
//                 break;
//             }
            $ids = json_decode(Input::get('ids'),1);
            $out_order_arr=array();
            $id_arr=array();
            foreach ($ids as $_id) {
                $moo=MaintenanceOutOrder::find($_id);
                if(is_null($moo)||$moo->shop_id!=$sc_user->shop_id){
                    $response = Lang::get("scs_error.maintenance_out_order_notexit_60009");
                    break 2;
                }
                if($moo->status!=0){
                    $response = Lang::get("scs_error.maintenance_out_order_statuserror_60010");
                    break 2;
                }
                $out_order_arr[]=$moo;
                $id_arr[]=array("id"=>$_id);
            }
            MaintenanceOutOrder::refuseOrder($out_order_arr);
            $response=array('code' => 0, 'msg' => 'ok', 'result' => array("id_info"=>$id_arr));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "implementOutOrder", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
}